/*
 Copyright (c) 2013, Ike Yousuf <admin@kudodev.com>
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 names of its contributors may be used to endorse or promote products
 derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.kudo.gdx.ui;

import com.badlogic.gdx.InputProcessor;
import com.kudo.gdx.ui.components.TextField;
import com.kudo.gdx.ui.components.Frame;
import com.kudo.gdx.Graphics;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author Ike
 */
public abstract class Container extends Component {

    private MultiInputProcessor input;
    private boolean active = false;
    private ZComparator zCompare = new ZComparator();
    private List<Component> children = new ArrayList<>();
    private boolean childrenDirty = true;
    private float minWidth = 50, minHeight = 50;
    private Component activeComponent = null;

    public Container() {
        input = new MultiInputProcessor(this);
    }

    public Container(float x, float y, float w, float h) {
        super(x, y, w, h);
        input = new MultiInputProcessor(this);
    }

    public Container(float x, float y) {
        super(x, y);
        input = new MultiInputProcessor(this);
    }

    public boolean contains(Component p) {
        return children.contains(p);
    }

    public void setActive(boolean b) {
        active = b;
    }

    public boolean getActive() {
        return active;
    }

    public void updateChildren() {
        for (Component c : children) {
            c.setLocation(c.getRelativeX(), c.getRelativeY());
            if (c instanceof Container) {
                ((Container) c).updateChildren();
            }
        }
    }

    @Override
    public void setParent(Container c) {
        parent = c;
        setLocation(getRelativeX(), getRelativeY());
        updateChildren();
    }

    @Override
    public void setBoundsLocation(float x, float y) {
        getBounds().x = x;
        getBounds().y = y;
        if (getParent() != null) {
            getRelativeLocation().x = getBounds().x - getParent().getX();
            getRelativeLocation().y = getBounds().y - getParent().getY();
        }
        updateChildren();
    }

    @Override
    public void render(Graphics g) {
        renderChildren(g);
    }

    public void renderChildren(Graphics g) {
        if (!getChildren().isEmpty()) {
            ensureZOrder();
            for (int i = 0; i < getChildren().size(); i++) {
                Component c = getChildren().get(i);
                if (c.getVisible()) {
                    c.render(g);
                    if (c instanceof Container) {
                        ((Container) c).renderChildren(g);
                    }
                }
            }
        }
    }

    @Override
    public void renderDebug(Graphics g) {
        if (!getChildren().isEmpty()) {
            ensureZOrder();
            for (int i = 0; i < getChildren().size(); i++) {
                if (getChildren().get(i).getVisible()) {
                    getChildren().get(i).renderDebug(g);
                }
            }
        }
    }

    private class ZComparator implements Comparator {

        @Override
        public int compare(Object o1, Object o2) {
            Component c1 = (Component) o1;
            Component c2 = (Component) o2;
            int res;
            if (c1 == null || c2 == null || c1.equals(c2)) {
                res = 0;
            } else {
                res = new Integer((int) c1.getZIndex()).compareTo((int) c2.getZIndex());
            }
            return res;
        }
    }

    public List<Component> getChildren() {
        return children;
    }

    public void add(Component c) {
        childrenDirty = true;
        c.setParent(this);
        c.setDisplay(getDisplay());
        updateProcessors(c);
        children.add(c);
    }

    public void updateProcessors(Component c) {
        if (c.getInput() != null) {
            input.addProcessor(c.getInput());
        }
    }

    @Override
    public MultiInputProcessor getInput() {
        return input;
    }

    public void remove(Component c) {
        childrenDirty = true;
        children.remove(c);
    }

    public void setChildrenDirty(boolean b) {
        childrenDirty = b;
    }

    public void ensureZOrder() {
        if (childrenDirty) {
            Collections.sort(children, zCompare);
            childrenDirty = false;
        }
    }

    public void packMinimumSize() {
        float w = 0, h = 0;
        for (Component c : getChildren()) {
            if (!(c instanceof Frame.ResizeButton)) {
                if (w < c.getWidth() + c.getRelativeX()) {
                    w = c.getWidth() + c.getRelativeX() + c.getRelativeX();
                    minWidth = w;
                }
                if (h < c.getHeight() + c.getRelativeY()) {
                    h = c.getHeight() + c.getRelativeY() + 5;
                    minHeight = h;
                }
            }
        }
    }

    public void setDisplay(UIDisplay d) {
        display = d;
        for (Component c : children) {
            c.setDisplay(d);
        }
    }

    public float getMinimalWidth() {
        return minWidth;
    }

    public float getMinimalHeight() {
        return minHeight;
    }
}
